import java.util.Vector;

public class EnemyTank extends Tank implements Runnable{
    Bullet bullet = null;
    Vector<Bullet> enemyBullets = new Vector<>();

    @Override
    public int getSpeed() {
        return speed;
    }

    @Override
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    int speed = 10;
    public boolean isLife() {
        return life;
    }

    public void setLife(boolean life) {
        this.life = life;
    }

    private boolean life = true;
    EnemyTank(int x, int y) {
        super(x, y);
        this.setType(1);
        this.setDirection(1);
    }
    public void up() {
        this.setY(this.getY() - this.getSpeed());
    }
    public void down() {
        this.setY(this.getY() + this.getSpeed());
    }
    public void left() {
        this.setX(this.getX() - this.getSpeed());
    }
    public void right() {
        this.setX(this.getX() + this.getSpeed());
    }
    int count = 0;
    //0向下 1向上 2向左 3向右
    public void attack() {
        //0向下 1向上 2向左 3向右
        switch(this.getDirection()) {
            case 0:
                bullet = new Bullet(this.getX() + 4,this.getY() + 28,this.getDirection());
                enemyBullets.add(bullet);
                break;
            case 1:
                bullet = new Bullet(this.getX() + 4,this.getY() - 22,this.getDirection());
                enemyBullets.add(bullet);
                break;
            case 2:
                bullet = new Bullet(this.getX() - 22,this.getY() + 8,this.getDirection());
                enemyBullets.add(bullet);
                break;
            case 3:
                bullet = new Bullet(this.getX() + 32,this.getY() + 8,this.getDirection());
                enemyBullets.add(bullet);
                break;
        }
        Thread thread = new Thread(bullet);
        thread.start();
//        System.out.println(bullet.getX() + " " + bullet.getY() + " " + bullet.getDirection());
    }
    public void randomMove() {
        if((int)(Math.random() * 4) == 3) {
            return;
        }
        if(!Map.scopeIf(this)) {
            setDirection((int) (Math.random() * 4));
            return;
        }
        count++;
        switch(getDirection()) {
            case 0:
                down();
                break;
            case 1:
                up();
                break;
            case 2:
                left();
                break;
            case 3:
                right();
                break;
        }
        if(count >= (int) (Math.random() * 40)) {
            setDirection((int) (Math.random() * 4));
            count = 0;
        }
    }

    @Override
    public void run() {
        while(isLife()) {
            try {
                Thread.sleep(500);
                randomMove();
                if(enemyBullets.size() < 5) {
                    int op = (int) (Math.random() * 10);
                    if(op == 5) attack();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //0向下 1向上 2向左 3向右

}
